package org.gc.webapp.action.user;

import org.apache.struts2.convention.annotation.Action;
import org.gc.common.entity.security.User;
import org.gc.common.vo.security.UserVO;
import org.gc.service.security.RoleService;
import org.gc.service.security.UserService;
import org.gc.webapp.action.BaseAction;
import org.gc.webapp.util.BizUtil;


public class UpProfileAction extends BaseAction{
	 private UserService userService;
	    private RoleService roleService;
	    private String oldpassword;
	    public String getOldpassword() {
			return oldpassword;
		}

		public void setOldpassword(String oldpassword) {
			this.oldpassword = oldpassword;
		}
		private UserVO user;
	    private Long id;
	    public void setUserService(UserService userService) {
	        this.userService = userService;
	    }

	    public void setRoleService(RoleService roleService) {
	        this.roleService = roleService;
	    }

	    public void setUser(UserVO user) {
	        this.user = user;
	    }

	    /**
	     * Return an instance of the user - to display when validation errors occur
	     * @return a populated user
	     */
	    public UserVO getUser() {
	        return user;
	    }
	    
	    @Action("edit")
	    public String edit(){
	    	id = BizUtil.getLoginUser().getId();
	        if(id != null){
	            user = userService.get(id);
	        }
	        return SUCCESS;
	    }
    @Action("updateProfile")
    public String updateProfile(){
    	UserVO oldUser = userService.get(BizUtil.getLoginUser().getId());
    	if(oldUser != null)
    	{
    		oldUser.setEmail(user.getEmail());
	    	oldUser.setPhoneNumber(user.getPhoneNumber());
	    	oldUser.setMobile(user.getMobile());
	    	oldUser.setName(user.getName());
	    	userService.save(oldUser);
    	}
    	
        return SUCCESS;
    }
    @Action("updatePassword")
    public String updatePassword(){
    	id = BizUtil.getLoginUser().getId();
        if(id != null){
        	UserVO oldUser = userService.get(id);
        	
            if(userService.updatePassword(oldUser, oldpassword, user.getPassword()))
            {
            	return SUCCESS;
            }
            else{
            	this.saveMessage("密码不正确");
            	return INPUT;
            }
            	
        }
        else return INPUT;
        
    }    
      
}
